package
{
	import flashx.textLayout.formats.Direction;
	
	import org.flixel.*;

	public class Player extends FlxSprite
	{
		/*[Embed(source="../media/shadowHumanTexture.png")] 
		static public var shadowHumanTexture:Class;*/
		/*[Embed(source="../media/maps/humanTexture.png")] 
		static public var humanTexture:Class;*/
		[Embed(source="../media/shadow.png")] 
		static public var shadowTexture:Class;
		
		//Jumping variables
		private var jumpStartY:int = 0;
		private var jumpPreviousY:int = 0;
		private var jumpHeight:int = 100;
		
		//Used for determining if the player can jump
		private var currentState:String = "Standing";
		
		//The current animation state
		private var currentAnimationState:String = "Walking";
		
		//Abilities
		private var abilities:Array = new Array();
		public var abilityIndex:int = 0;
		
		//Variable telling if the player is currently fading
		public var fading:Boolean = false;
		
		//Variables used for moving the player from outside the class
		private var movementLeft:FlxPoint = new FlxPoint();
		public var autoMoving:Boolean = false;
		
		public function Player()
		{
			loadGraphic(shadowTexture,true,true,32,64,false);
			x = 50;
			y = 0;
			acceleration.y = 400;
			addAnimation("Walking",[0,1,2,3,4,5,6,7],8,true);
			facing = LEFT;
			play("Walking");
		}
		
		override public function update():void
		{
			super.update();
			if(movementLeft.x == 0 && movementLeft.y == 0)
			{
				if(FlxG.keys.Q)
				{
					handleAbilities();
				}
				else if(currentAnimationState != "Walking")
				{
					play("Walking");
				}
				if(FlxG.keys.D)
				{
					x+=2;
					facing = LEFT;
				}
				if(FlxG.keys.A)
				{
					x-=2;
					facing = RIGHT;
				}
				if(FlxG.keys.SPACE && currentState == "Standing")
				{
					jumpStartY = y;
					acceleration.y = 0;
					velocity.y = -150;
					currentState = "Jumping";
				}
				if((!FlxG.keys.SPACE && currentState == "Jumping") || 
					(currentState == "Jumping" && (jumpStartY-y >= jumpHeight || jumpPreviousY == jumpStartY-y)))
				{
					jumpStartY = 0;
					acceleration.y = 400;
					currentState = "InAir";
				}
				else if(velocity.y == 0 && currentState == "InAir")
				{
					currentState = "Standing";
					jumpStartY = 0;
				}
				jumpPreviousY = jumpStartY-y;
			}
			else
			{
				handleAutoMovement();
			}
		}
		private function handleAbilities():void
		{
			var currentAbility:String = abilities[abilityIndex];
			if(currentAbility == "Teleport")
			{
				
			}
			else if(currentAbility == "Run")
			{
				
			}
		}
		private function handleAutoMovement():void
		{
			if(movementLeft.x<0)
			{
				movementLeft.x++;
				x++;
			}
			else if(movementLeft.x>0)
			{
				movementLeft.x--;
				x--;
			}
			else if(movementLeft.y<0)
			{
				movementLeft.y++;
				y++;
			}
			else if(movementLeft.y>0)
			{
				movementLeft.y--;
				y--;
			}
			if(movementLeft.x ==0 && movementLeft.y == 0)
			{
				autoMoving = false;
			}
		}
		public function setMovement(x:int,y:int):void
		{
			movementLeft = new FlxPoint(x,y);
			autoMoving = true;
		}
		public function fadeIn():void
		{
			
		}
		public function fadeOut():void
		{
			
		}
		public function setAbilities(type:String):void
		{
			if(type == "Shadow")
			{
				abilities = new Array();
				abilities.push("Teleport");
				abilities.push("Run");
			}
			else if(type == "ShadowHuman")
			{
				
			}
			else if(type == "Human")
			{
				
			}
		}
	}
}